package com.IntelligentCommunity.community.controller;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.poi.ExcelUtil;
import com.IntelligentCommunity.common.core.web.controller.BaseController;
import com.IntelligentCommunity.common.core.web.domain.AjaxResult;
import com.IntelligentCommunity.common.core.web.page.TableDataInfo;
import com.IntelligentCommunity.common.log.annotation.Log;
import com.IntelligentCommunity.common.log.enums.BusinessType;
import com.IntelligentCommunity.common.security.annotation.RequiresPermissions;
import com.IntelligentCommunity.community.domain.*;
import com.IntelligentCommunity.community.service.IRealPopulationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 实有人口统计Controller
 *
 * @author zzpjx
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/community")
public class RealPopulationController extends BaseController
{
    @Autowired
    private IRealPopulationService realPopulationService;

    /**
     * 全量同步人脸到算法库
     * @return
     */
    @GetMapping("/syncFace2Algorithm")
    public R syncFace2Algorithm() {
        realPopulationService.syncFace2Algorithm();
        return R.ok();
    }

    /**
     * 查询实有人口统计列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RealPopulation realPopulation) {
        startPage();
        List<RealPopulation> list = realPopulationService.selectRealPopulationList(realPopulation);
        return getDataTable(list);
    }

    /**
     * 导出实有人口统计列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, RealPopulation realPopulation)
    {
        List<RealPopulation> list = realPopulationService.selectRealPopulationList(realPopulation);
        ExcelUtil<RealPopulation> util = new ExcelUtil<RealPopulation>(RealPopulation.class);
        util.exportExcel(response, list, "实有人口统计数据");
    }

    /**
     * 导入实有人口统计
     */
    @PostMapping("/import")
    @Transactional
    public R handleFileUpload(@RequestParam("file") MultipartFile file) {
        ExcelUtil excelUtil = new ExcelUtil(RealPopulation.class);
        try {
            List<RealPopulation> list = excelUtil.importExcel(file.getInputStream());
            if (CollectionUtils.isEmpty(list)) {
                throw new RuntimeException("文件内容为空");
            }

            int batchSize = 5000; // 调整为适合的批量大小
            int batchCount = (list.size() + batchSize - 1) / batchSize;

            for (int i = 0; i < batchCount; i++) {
                int fromIndex = i * batchSize;
                int toIndex = Math.min(fromIndex + batchSize, list.size());
                List<RealPopulation> batchList = list.subList(fromIndex, toIndex);
                CompletableFuture.runAsync(() -> realPopulationService.insertRealPopulationList(batchList));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }



    /**
     * 获取实有人口统计详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(realPopulationService.selectRealPopulationById(id));
    }

    /**
     * 新增实有人口统计
     */
    @Log(title = "实有人口统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RealPopulation realPopulation){
        return toAjax(realPopulationService.insertRealPopulation(realPopulation));
    }

    /**
     * 修改实有人口统计
     */
    @Log(title = "实有人口统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RealPopulation realPopulation)
    {
        return toAjax(realPopulationService.updateRealPopulation(realPopulation));
    }

    /**
     * 删除实有人口统计
     */
    @Log(title = "实有人口统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(realPopulationService.deleteRealPopulationByIds(ids));
    }

    /**
     * 功能：实有车辆户主下拉框查询
     * @return
     */
    @GetMapping("/populationList")
    public R populationList(){
        List<RealPopulation> list = realPopulationService.populationList();
        return R.ok(list);
    }


    @PostMapping("/updateTimeById/{id}")
    public R updateTimeById(@PathVariable("id") Long id){
        realPopulationService.updateTimeById(id);
        return R.ok();
    }

    /**
     * 功能：查询人员类型下拉框查询
     * @return
     */
    @RequiresPermissions("community:population:populationType")
    @GetMapping("/populationType")
    public R populationType(){
        List<PopulationType> list = realPopulationService.populationType();
        return R.ok(list);
    }

    /**
     * 功能：查询人员标签
     * @return
     */
    @RequiresPermissions("community:population:labelList")
    @GetMapping("/labelList")
    public R labelList(){
        List<RealLabel> list = realPopulationService.labelList();
        return R.ok(list);
    }

    /**
     * 功能：人员迁出
     */
    @PostMapping("/handleEmigration")
    public R handleEmigration(@RequestBody Emigration emigration){
        realPopulationService.handleEmigration(emigration);
        return R.ok();
    }

    /**
     * 功能：查询人员迁出日志
     */
    @PostMapping("/EmigrationLog")
    public R selectEmigrationLog(@RequestParam Integer id){
        List<Emigration> emigrations = realPopulationService.selectEmigrationLog(id);
        return R.ok(emigrations);
    }

    /**
     * 功能：根据小区id查询楼栋
     */
    @PostMapping("/listRidgepole")
    public R listRidgepole(@RequestParam Integer id){
        List<RealEstate> emigrations = realPopulationService.selectRidgepole(id);
        return R.ok(emigrations);
    }

    /**
     * 功能：根据楼栋查询单元
     */
    @PostMapping("/getElementList")
    public R getElementList(@RequestParam Integer id,@RequestParam Integer did){
        List<RealEstate> emigrations = realPopulationService.selectElement(id,did);
        return R.ok(emigrations);
    }

    /**
     * 功能：根据楼栋查询单元
     */
    @PostMapping("/getHouseholdsList")
    public R getHouseholdsList(@RequestParam Integer element,
                               @RequestParam Integer ridgepole,
                               @RequestParam Integer did){
        List<RealEstate> emigrations = realPopulationService.selectHouseholds(element,ridgepole,did);
        return R.ok(emigrations);
    }

    /**
     * 功能：查询当前小区下的实有人口
     */
    @GetMapping("/selPopulation")
    public R selPopulation(@RequestParam Integer districtId){
        List<RealPopulation> list = realPopulationService.selPopulation(districtId);
        return R.ok(list);
    }
}
